﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using HWmanagementDB;

namespace HWmanagementServer
{
    //for manipulating messages. Message objects may be acquired form the DB
    //or may be created by the client
    public class MessageMgr
    {
        //For sending a message -- TESTED, OK
        public static void sendMessage(MessageModel msg)
        {
            try
            {
                using (Repository repo = new Repository())
                {
                    //queriing the last id, ad setting it up
                    int msgid = 0;
                    try
                    {
                        IEnumerator<MESSAGE> MsgEnum = repo.MessageRepository.Get
                            (
                            order: (u) => u.OrderByDescending((u2) => u2.ID)
                            ).GetEnumerator();

                        MsgEnum.MoveNext();
                        msgid = MsgEnum.Current.ID + 1;
                    }
                    catch (NullReferenceException)
                    {
                        //if null is returned by the query
                        msgid = 1;
                    }

                    msg.id = msgid;

                    //writing the message to the DB
                    repo.MessageRepository.Insert(MessageModel.toDBTable(msg));
                    return;
                }
            }
            catch
            {
                Program.log.LogToFile("Error sending message with id: " + msg.id);
                throw new Exception("Error while handling request!");
            }
        }

        //getting the messages sent for the user -- TESTED, OK
        public static List<MessageModel> getMessages(string user)
        {
            try
            {
                using (Repository repo = new Repository())
                {
                    List<MessageModel> ret = new List<MessageModel>();
                    IEnumerator<MESSAGE> msgs = repo.MessageRepository.Get
                        (
                        filter: (m) => m.RECEIVER == user || m.SENDER == user
                        ).GetEnumerator();

                    //converting the messages to model
                    while (msgs.MoveNext())
                    {
                        ret.Add(MessageModel.fromDBTable(msgs.Current));
                    }

                    return ret;
                }
            }
            catch
            {
                Program.log.LogToFile("Error getting messages with for user: " + user);
                throw new Exception("Error while handling request!");
            }
        }

        //getting the list of users for sendig a message
        public static List<UserModel> getAllUser()
        {
            List<UserModel> ret=new List<UserModel>();
            try
            {
                using (Repository repo = new Repository())
                {
                    IEnumerator<USER> usrs = repo.UserRepository.Get
                        (
                            order: (u) => u.OrderByDescending((u2) => u2.NEPTUN)
                        ).GetEnumerator();

                    while (usrs.MoveNext())
                    {
                        ret.Add(UserModel.fromDBTable(usrs.Current));
                    }

                    return ret;
                }
            }
            catch
            {
                Program.log.LogToFile("Error getting list of users!");
                throw new Exception("Error while handling request!");
            }
        }

        //setting the is_read attribute of a message
        public static void setRead(int msg_id,bool read)
        {
            try
            {
                using (Repository repo = new Repository())
                {
                    //getting the message
                    MESSAGE msg = repo.MessageRepository.GetByID(msg_id);
                    msg.ISREAD = read;
                    
                    //updating the database, if the value changed
                    if (msg.ISREAD != read)
                    {
                        repo.MessageRepository.Update(msg);
                    }
                }
            }
            catch
            {
                Program.log.LogToFile("Setting is_read on message "+msg_id+" failed!");
                throw new Exception("Error while handling request!");
            }
              
        }
    }
}
